home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / Tool Chest / Development Platforms / AppsToGo / AppsToGo.src / AppWannabe / Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-18  |  9.3 KB  |  343 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        Menu.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  26. #include "App.defs.h"        /* Get various application definitions.            */
  27. #include "App.protos.h"        /* Get the prototypes for application.            */
  28.  
  29. #ifndef __DESK__
  30. #include <Desk.h>
  31. #endif
  32.  
  33. #ifndef __ERRORS__
  34. #include <Errors.h>
  35. #endif
  36.  
  37. #ifndef __MEMORY__
  38. #include <Memory.h>
  39. #endif
  40.  
  41. #ifndef __MENUS__
  42. #include <Menus.h>
  43. #endif
  44.  
  45. #ifndef __TOOLUTILS__
  46. #include <ToolUtils.h>
  47. #endif
  48.  
  49. #ifndef __UTILITIES__
  50. #include "Utilities.h"
  51. #endif
  52.  
  53.  
  54.  
  55. /*****************************************************************************/
  56.  
  57.  
  58.  
  59. extern Boolean    gQuitApplication;
  60. extern Boolean    gHasAppleEvents;
  61. extern OSType    gAppWindowType;
  62.  
  63. extern Boolean    gLowOnMem;
  64.  
  65.  
  66.  
  67. /*****************************************************************************/
  68. /*****************************************************************************/
  69.  
  70.  
  71.  
  72. /* •• Called by DTS.Lib..framework. •• */
  73.  
  74. /* Adjust the menu items.  We allow the DTS.Lib framework to do most of the work
  75. ** for us.  It will walk the menubar, and for each menu in the menubar, it will
  76. ** disable all of the menu items and then call the application at either
  77. ** AdjustMenuItems() (for document and palette windows, or for the no-window case),
  78. ** or DialogAdjustMenuItems() (for modal dialogs).  The application's job is to then
  79. ** turn on menu items that should be enabled to match the current application state.
  80. ** The initial Wannabe code for AdjustMenuItems() calls DoAdjustFileMenu() for the
  81. ** file menu, and DoAdjustEditMenu() for the edit menu.  Any other menus that may
  82. ** be added to Wannabe have all menu items enabled.  This allows menus to be added
  83. ** to the running version of Wannabe and allows them to actually do something.
  84. ** If the top-most window is a dialog, then all menus are disabled except for the
  85. ** edit menu.  Various items in the edit menu are enabled, depending on if there
  86. ** is an active TextEdit control, and what is in the clipboard. */
  87.  
  88. #pragma segment Menu
  89. void    DoAdjustMenus(void)
  90. {
  91.     if (DoAdjustMBARMenus(FrontWindow(), rMenuBar))
  92.         DrawMenuBar();
  93. }
  94.  
  95.  
  96.  
  97. /*****************************************************************************/
  98.  
  99.  
  100.  
  101. /* This is called when an item is chosen from the menu bar (after calling
  102. ** MenuSelect or MenuKey).  It performs the right operation for each command.
  103. ** It is good to have both the result of MenuSelect and MenuKey go to one
  104. ** routine like this to keep everything organized. */
  105.  
  106. #pragma segment Menu
  107. Boolean    DoMenuCommand(short menuID, short menuItem)
  108. {
  109.     short        undoDepth, numUndos, saveMode, daRefNum;
  110.     Str255        daName;
  111.     FileRecHndl    frHndl;
  112.     WindowPtr    window;
  113.     TEHandle    te;
  114.     OSErr        err;
  115.     Boolean        handled;
  116.  
  117.     handled = true;
  118.  
  119.     if (window = FrontWindow())
  120.         frHndl = (FileRecHndl)GetWRefCon(window);
  121.             /* frHndl is valid only if it is one of our windows. */
  122.  
  123.     switch (menuID) {
  124.  
  125.         case mApple:
  126.             switch (menuItem) {
  127.                 case kStdAbout:        /* Bring up alert for About. */
  128.                     NewDocumentWindow(nil, 'ABOT', false);
  129.                     break;
  130.                 default:            /* All non-About items in this menu are DAs. */
  131.                     GetItem(GetMHandle(mApple), menuItem, daName);
  132.                     daRefNum = OpenDeskAcc(daName);
  133.                     break;
  134.             }
  135.             break;
  136.  
  137.         case mFile:
  138.             switch (menuItem) {
  139.                 case kStdNew:
  140.                     if (err = NewDocumentWindow(nil, gAppWindowType, true))
  141.                         HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  142.                     break;
  143.                 case kStdOpen:
  144.                     err = OpenDocumentWindow(&frHndl, nil, fsRdWrPerm);
  145.                     if ((err) && (err != userCanceledErr))
  146.                         HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  147.                     break;
  148.                 case kStdClose:
  149.                     if (IsAppWindow(window)) {
  150.                         if (window = FrontWindowOfType(kwIsDocument, true))
  151.                             DisposeOneWindow(window, kClose);
  152.                     }
  153.                     else
  154.                         DisposeOneWindow(window, kClose);        /* Dispose of DA window. */
  155.                     break;
  156.                 case kStdSave:
  157.                 case kStdSaveAs:
  158.                     saveMode = (menuItem == kStdSave) ? kSave : kSaveAs;
  159.                     if ((*frHndl)->fileState.refNum == kInvalRefNum)
  160.                         saveMode = kSaveAs;
  161.                     err = SaveDocument(frHndl, window, saveMode);
  162.                     if ((err) && (err != userCanceledErr))
  163.                         HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  164.                     break;
  165.                 case kStdPageSetup:
  166.                     DoSetCursor(&qd.arrow);
  167.                     PresentStyleDialog(frHndl);
  168.                     break;
  169.                 case kStdPrint:
  170.                     DoSetCursor(&qd.arrow);
  171.                     err = noErr;
  172.                     if (!(*frHndl)->d.doc.fhInfo.printRecValid)
  173.                         err = PresentStyleDialog(frHndl);
  174.                     if (!err) {
  175.                         err = PrintDocument(frHndl, true, true);
  176.                         PrintDocument(nil, false, false);
  177.                     }
  178.                     if ((err) && (err != userCanceledErr))
  179.                         HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  180.                     break;
  181.                 case kStdQuit:
  182.                     gQuitApplication = DisposeAllWindows();
  183.                     break;
  184.                 default:
  185.                     handled = false;
  186.                     break;
  187.             }
  188.             break;
  189.  
  190.         case mEdit:            /* Call SystemEdit for DA editing & MultiFinder. */
  191.             if (menuItem == kStdViewHier) {
  192.                 handled = false;
  193.                 break;
  194.             }
  195.             if (IsAppWindow(window)) {
  196.                 switch (menuItem) {
  197.                     case kStdUndo:
  198.                     case kStdRedo:
  199.                     case kStdCut:
  200.                     case kStdCopy:
  201.                     case kStdPaste:
  202.                     case kStdClear:
  203.                         switch ((*frHndl)->fileState.sfType) {
  204.                                 /* This is written with the assumption that document types
  205.                                 ** that demand specific code will be added.  The below “if”
  206.                                 ** illustrates how to handle the edit menu for windows that
  207.                                 ** have an active TextEdit control.  The “else” shows a typical
  208.                                 ** undo/redo scenario for applications that are using the
  209.                                 ** hierarchical document package.  The clipboard features
  210.                                 ** are of course document-dependent, so a sample hasn't been
  211.                                 ** implemented here.  For a sample, see DTS.Draw. */
  212.                             default:
  213.                                 if (te = CTEFindActive(window)) {
  214.                                     if ((*te)->viewRect.left < -8192)
  215.                                         BeginFrame(window);
  216.                                     else
  217.                                         BeginContent(window);
  218.                                     if (menuItem == kStdUndo)
  219.                                         CTEUndo();
  220.                                     else
  221.                                         CTEClipboard(menuItem - kStdCut + 2);
  222.                                     EndContent(window);
  223.                                 }
  224.                                 else {
  225.                                     if (menuItem <= kStdRedo) {
  226.                                         if (!UnmapMItem(mEdit, kStdUndo)) {
  227.                                             GetUndoInfo(frHndl, &undoDepth, &numUndos);
  228.                                             DoUndoTask((*frHndl)->d.doc.root, 1 - undoDepth, true);
  229.                                         }
  230.                                         else DoUndoTask((*frHndl)->d.doc.root, menuItem - kStdUndo, true);
  231.                                     }
  232.                                     else {
  233.                                         /* Handle rest of edit menu here. */
  234.                                     }
  235.                                 }
  236.                                 break;
  237.                         }
  238.                         break;
  239.                 }
  240.             }
  241.             else SystemEdit(menuItem - 1);
  242.             break;
  243.  
  244.         default:
  245.             handled = false;
  246.             break;
  247.  
  248.     }
  249.  
  250.     return(handled);
  251. }
  252.  
  253.  
  254.  
  255. /*****************************************************************************/
  256.  
  257.  
  258.  
  259. #pragma segment Menu
  260. Boolean    DoAdjustFileMenu(WindowPtr window)
  261. {
  262.     MenuHandle    menu;
  263.     FileRecHndl    frHndl;
  264.     short        enableItem;
  265.  
  266.     menu = GetMHandle(mFile);
  267.     EnableItem(menu, UnmapMItem(mFile, kStdQuit));            /* Gotta be able to quit. */
  268.  
  269.     if (IsDAWindow(window)) {
  270.         EnableItem(menu, UnmapMItem(mFile, kStdClose));        /* Let DAs do a close from the menu. */
  271.         return(false);
  272.     }
  273.  
  274.     if (!gLowOnMem) {
  275.         EnableItem(menu, UnmapMItem(mFile, kStdNew));
  276.         EnableItem(menu, UnmapMItem(mFile, kStdOpen));
  277.     }
  278.  
  279.     if (window = FrontWindowOfType(kwIsDocument, true)) {
  280.         EnableItem(menu, UnmapMItem(mFile, kStdClose));
  281.         frHndl = (FileRecHndl)GetWRefCon(window);
  282.         if ((*frHndl)->fileState.sfType == kDocFileType) {
  283.             enableItem = GetWindowDirty(window);
  284.             if ((*frHndl)->fileState.refNum == kInvalRefNum)
  285.                 enableItem = true;
  286.             if (enableItem)
  287.                 EnableItem(menu, UnmapMItem(mFile, kStdSave));
  288.             EnableItem(menu, UnmapMItem(mFile, kStdSaveAs));
  289.         }
  290.         EnableItem(menu, UnmapMItem(mFile, kStdPageSetup));
  291.         EnableItem(menu, UnmapMItem(mFile, kStdPrint));
  292.     }
  293.  
  294.     return(false);
  295. }
  296.  
  297.  
  298.  
  299. /*****************************************************************************/
  300.  
  301.  
  302.  
  303. #pragma segment Menu
  304. Boolean    DoAdjustEditMenu(WindowPtr window)
  305. {
  306.     MenuHandle        menu;
  307.     Boolean            menuEnabled;
  308.     FileRecHndl        frHndl;
  309.  
  310.     menu = GetMHandle(mEdit);
  311.  
  312.     if (IsDAWindow(window)) {
  313.         EnableItem(menu, UnmapMItem(mEdit, kStdUndo));
  314.         EnableItem(menu, UnmapMItem(mEdit, kStdCut));
  315.         EnableItem(menu, UnmapMItem(mEdit, kStdCopy));
  316.         EnableItem(menu, UnmapMItem(mEdit, kStdPaste));
  317.         EnableItem(menu, UnmapMItem(mEdit, kStdClear));
  318.         return(false);
  319.     }
  320.  
  321.     if (IsAppWindow(window)) {
  322.         frHndl = (FileRecHndl)GetWRefCon(window);
  323.         switch ((*frHndl)->fileState.sfType) {
  324. #if VH_VERSION
  325.             case kViewHierFileType:
  326.                 CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
  327.                                                  UnmapMItem(mEdit, kStdCut));
  328.                 break;
  329. #endif
  330.             default:
  331.                 EnableItem(menu, UnmapMItem(mEdit, kStdViewHier));
  332.                 CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
  333.                                                  UnmapMItem(mEdit, kStdCut));
  334.                 break;
  335.         }
  336.     }
  337.  
  338.     return(false);
  339. }
  340.  
  341.  
  342.  
  343.